/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：NavCfgController.java
 * History:
 *         2012-6-26: Initially created, Chrise.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.base.SystemContent;
import com.xteam.ecsystem.entities.SysNavConfig;
import com.xteam.ecsystem.service.admin.NavCfgService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.PageCenterStation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 导航配置控制层。
 * @author Chrise
 */
@Controller
public class NavCfgController {
	
	@Autowired
	private NavCfgService ncService;
	
	@Autowired
	private SystemLogService logService;
	
	/**
	 * 获取导航配置。
	 * @author Chrise 2012-6-26
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getNavConfigs.do")
	public void getNavConfigs(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int sncType = ReqHelper.getRequestInt(request, "sncType");
		String sncName = ReqHelper.getRequestString(request, "sncName");
		int sncPage = ReqHelper.getRequestInt(request, "sncPage");
		int sncPosition = ReqHelper.getRequestInt(request, "sncPosition");
		int sncState = ReqHelper.getRequestInt(request, "sncState");
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		
		PageCenterStation<SysNavConfig> pcs = this.ncService.getNavConfigs(sncType, sncName, 
			sncPage, sncPosition, sncState, pageIndex, pageSize);
		JSONObject result = JSONObject.fromObject(pcs);
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 切换导航配置状态。
	 * @author Chrise 2012-6-26
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/switchNcState.do")
	public void switchNcState(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		JSONObject object = JSONObject.fromObject(data);
		
		this.ncService.switchState(object.getString("sncId"), object.getBoolean("sncTo"));
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_NAVCFG_MGR, 
				"switchState", ConstantLocation.LOG_Edit_STATE, "SysNavConfig");
	}
	
	/**
	 * 删除导航配置。
	 * @author Chrise 2012-6-26
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/delNavConfigs.do")
	public void delNavConfigs(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		JSONArray array = JSONArray.fromObject(data);
		
		List<String> ids = new ArrayList<String>();
		for (int index = 0; index < array.size(); index ++) {
			JSONObject object = array.getJSONObject(index);
			ids.add(object.getString("sncId"));
		}
		
		this.ncService.delNavConfigs(ids);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_NAVCFG_MGR, 
				"delNavConfigs", ConstantLocation.LOG_DELETE_CFG, "SysNavConfig");
	}
	
	/**
	 * 保存导航配置。
	 * @author Chrise 2012-6-26
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/saveNavConfig.do")
	public void saveNavConfig(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD_CFG;
		
		JSONObject object = JSONObject.fromObject(data);
		SysNavConfig snc = (SysNavConfig)JSONObject.toBean(object, SysNavConfig.class);
		
		JSONObject result = new JSONObject();
		if (this.ncService.saveNavConfig(snc)) {
			result.put("issuccess", true);
			
			if(StringUtil.isNotNullOrEmpty(object.get("sncId"))){
				lslType = ConstantLocation.LOG_EDIT_CFG;
			}
			
			this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_NAVCFG_MGR, 
					"saveNavConfig", lslType, "SysNavConfig");
		} else {
			result.put("issuccess", false);
			result.put("message", "已存在相同显示名称的配置！");
		}
		
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 获取导航配置。
	 * @author Chrise 2012-6-26
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getNavConfig.do")
	public void getNavConfig(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		JSONObject object = JSONObject.fromObject(data);
		
		SysNavConfig snc = this.ncService.getNavConfig(object.getString("sncId"));
		
		JSONObject result = new JSONObject();
		if (snc != null) {
			JSONObject bean = JSONObject.fromObject(snc);
			
			result.put("issuccess", true);
			result.put("message", bean.toString());
		} else {
			result.put("issuccess", false);
			result.put("message", "指定配置不存在！");
		}
		
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 获取导航内容。
	 * @author Chrise 2012-6-26
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getNavContent.do")
	public void getNavContent(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int type = ReqHelper.getRequestInt(request, "type");
		
		List<SystemContent> list = this.ncService.getNavContent(type);
		JSONArray result = JSONArray.fromObject(list);
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 获取导航内容。
	 * @author Chrise 2012-6-29
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getNavContents.do")
	public void getNavContents(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int scType = ReqHelper.getRequestInt(request, "scType");
		String scName = ReqHelper.getRequestString(request, "scName");
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		
		PageCenterStation<SystemContent> pcs = this.ncService.getNavContents(scType, scName, pageIndex, pageSize);
		JSONObject result = JSONObject.fromObject(pcs);
		response.getWriter().write(result.toString());
	}
}
